package com.bosch.in.rtp1.ept.manager.impl;

import java.sql.SQLException;

import com.bosch.in.rtp1.ept.constant.SkippedRecipeConstanst;
import com.bosch.in.rtp1.ept.dao.SkpFshRcpConDao;
import com.bosch.in.rtp1.ept.manager.SkpFshRcpManager;
import com.bosch.in.rtp1.ept.model.dto.SkippedFshRcpCon;
import com.bosch.in.rtp1.ept.util.DaoFactory;
import com.bosch.in.rtp1.ept.util.ehcache.EhcacheFactory;

/**
 * Concrete implementation of SkpFshRcpManager which provides service to manipulate 
 * SkippedFshRcpCon data, delegate data source retrieval and transaction management to 
 * DBConnectionManager attribute.  
 * @author LIV1HC
 *
 */

public class SkpFshRcpConManagerImpl implements SkpFshRcpManager{
	
	/**
	 * used to manage connection
	 */
	private DBConnectionManager dbManager;
	
	/**
	 * dao object
	 */
	private SkpFshRcpConDao skFinishRecipeDao;
	
	public SkpFshRcpConManagerImpl(){
	}

	@Override
	public SkippedFshRcpCon getSkpFshRcpCon() throws SQLException {
		try
		{
			return this.getSkippedFshRcpCon();
		}
		finally
		{
			if (dbManager != null)
			{
				dbManager.close();
			}
		}
	}


	@Override
	public boolean updateSkippedFshRcpCon(SkippedFshRcpCon oldSkippedFshRcpCon, SkippedFshRcpCon newSkippedFshRcpCon) throws SQLException
			{
		try
		{
			// lazy init connection
			init();
			return skFinishRecipeDao.updateSkpFshRcpCon(oldSkippedFshRcpCon, newSkippedFshRcpCon);
		}
		finally
		{
			dbManager.commit();
			dbManager.close();
		}
	}


	@Override
	public boolean insertSkippedFshRcpCon(SkippedFshRcpCon skippedFshRcpCon) throws SQLException
			 {
		try
		{
			// lazy init connection
			init();
			boolean result = false;
			result = skFinishRecipeDao.insertSkpFshRcpCon(skippedFshRcpCon);
			if (skippedFshRcpCon.isApplyForAll())
			{
				result = result && skFinishRecipeDao.updateSkpCol();
			}
			if (result)
			{
				//invalidate cache
				EhcacheFactory.getSkippedRecipeCon().invalidate(SkippedRecipeConstanst.SKIPPED_FSH_RCP_CACHE);
				//invalidate finished recipe chart cache
				EhcacheFactory.getFshRcpFactorEhcacheWrapper().invalidate();
				EhcacheFactory.getFshRcpRecords().invalidate();
			}
			return result;
		}
		finally
		{
			dbManager.commit();
			dbManager.close();
		}
	}
	
	/***********PRIVATE METHOD****************/
	
	/**
	 * lazy initialize connection when we really need to interact with database and in case 
	 * data can't be found in caching system.
	 * @throws SQLException
	 */
	private void init() throws SQLException {
		dbManager = new DBConnectionManager();
		skFinishRecipeDao = DaoFactory.getInstance().getSkpFshRcpConDao(dbManager.getConnection());
	}

	/**
	 * Try to load SkippedFshRcpCon from cache first. 
	 * If it is not found, try loading from database
	 * @return SkippedFshRcpCon
	 * @throws SQLException
	 */
	private SkippedFshRcpCon getSkippedFshRcpCon() throws SQLException
	{
		// try to load SkippedFshRcp from cache	
		SkippedFshRcpCon sFshRcpCon = EhcacheFactory.getSkippedRecipeCon().get(SkippedRecipeConstanst.SKIPPED_FSH_RCP_CACHE);
		if (sFshRcpCon == null)
			{
				// lazy init connection
				init();
				// load SkippedFshRcp from database if it is not in cache  
				sFshRcpCon = skFinishRecipeDao.getSkpFshRcpCon();
				//put chartFactors list into cache
				if (sFshRcpCon != null)
				{
					EhcacheFactory.getSkippedRecipeCon().put(SkippedRecipeConstanst.SKIPPED_FSH_RCP_CACHE, sFshRcpCon);
				}
			}
		return sFshRcpCon;
	}
	

}
